package com.example.demo.util;

import com.aliyun.oss.HttpMethod;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.GeneratePresignedUrlRequest;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.net.URL;
import java.util.Date;

public class UploadALiYunOOS {

    private String accessKeyId="gqiOZ5IrrRAa87yG";
    private String accessKeySecret="hugCZ1hf4lk6yzCirsaQJcObhnXveO";
    private String bucketName="artonetest";
    private String endPoint="oss-cn-hangzhou.aliyuncs.com";

    public OSSClient creatOSSClient() {
        OSSClient ossClient = null;
        try {
            ossClient = new OSSClient(endPoint, accessKeyId, accessKeySecret);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  ossClient;
    }

    public void  shutDownClient(OSSClient ossClient){
        if (ossClient != null)
            ossClient.shutdown();
    }

    //文件直接上传的方式，filename 为定义的文件名字
    public void upload(OSSClient ossClient, File obj, String fileName) {
        long start = System.currentTimeMillis();
        try {
            ossClient.putObject(bucketName, fileName, obj);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            System.out.print("上传时间：{}ms"+ (System.currentTimeMillis() - start));
        }
    }


    //文件字节方式进行上传，filename 为定义的文件名字
    public void upload(OSSClient ossClient,byte[] array, String fileName) {
        long start = System.currentTimeMillis();
        try {
            ossClient.putObject(bucketName, fileName, new ByteArrayInputStream(array));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.print("上传时间：{}ms"+ (System.currentTimeMillis() - start));
        }
    }

    public String query(String objectName){
        // 创建OSSClient实例。
        OSS ossClient = new OSSClient(endPoint, accessKeyId, accessKeySecret);
        // 设置URL过期时间为1小时。
        Date expiration = new Date(new Date().getTime() + 3600 * 1000);
        // 生成以GET方法访问的签名URL，访客可以直接通过浏览器访问相关内容。
        URL url = ossClient.generatePresignedUrl(bucketName, objectName, expiration);
        System.out.print(url.toString());
        // 关闭OSSClient。
        ossClient.shutdown();
        return url.toString();
    }


    public String styleQuery(OSSClient ossClient,String objectName){
        // 设置图片处理样式。
        String style = "style/yasuopic";
        // 指定过期时间为10分钟。
        Date expiration = new Date(new Date().getTime() + 1000 * 60 * 30 );
        GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucketName, objectName, HttpMethod.GET);
        req.setExpiration(expiration);
        req.setProcess(style);
        URL signedUrl = ossClient.generatePresignedUrl(req);
        //  System.out.println(signedUrl.toString());
        return signedUrl.toString();
    }

    public String styleQuery(OSSClient ossClient,String objectName,String styles){
        // 指定过期时间为10分钟。
        Date expiration = new Date(new Date().getTime() + 1000 * 60 * 30 );
        GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucketName, objectName, HttpMethod.GET);
        req.setExpiration(expiration);
        req.setProcess(styles);
        URL signedUrl = ossClient.generatePresignedUrl(req);
        //  System.out.println(signedUrl.toString());
        return signedUrl.toString();
    }

}
